package com.oa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.bean.CarApply;
import com.oa.bean.CarManage;
import com.oa.service.CarManageService;

@Controller
public class CarController {

	@Autowired
	CarManageService carManageService;
	
	@RequestMapping("addCarManage")
	@ResponseBody
	public String addCarManage(CarManage carManage){
		System.out.println(carManage);
		String result = carManageService.addCar(carManage);		
		return result;
	}
	
	@RequestMapping("deleteCarManage")
	@ResponseBody
	public String deleteCarManage(Integer cm_id){
		System.out.println(cm_id);
		String result = carManageService.MoveCar(cm_id);
		return result;
	}
	
	@RequestMapping("getCarManageList")
	@ResponseBody
	public Map<String,Object>getCarManageList(Integer currentPage){
		Map<String,Object> map = new HashMap<>();
		map.put("currentPage", currentPage);
		Map<String,Object> result = carManageService.getCarManageList(map);
		return result;
	}
	
	@RequestMapping("addApplyCar")
	public String addApplyCar(CarApply carApply, HttpSession session){
		System.out.println(carApply);
		Integer uid = (Integer) session.getAttribute("uid");
		carApply.setCa_uid(uid);
		String result = carManageService.addCarApply(carApply);
		if("true".equals(result)){
			return "redirect:Template/carApply.jsp";
		}
		return "";
	}
	
	@RequestMapping("getCnameList")
	@ResponseBody
	public List<CarManage> getCnameList(){
		List<CarManage> list = carManageService.getCnameList();
		return list;
	}
	
	@RequestMapping("getMyCar")
	@ResponseBody
	public List<CarApply> getMyCar(HttpSession session){
		Integer uid = (Integer) session.getAttribute("uid");
		List<CarApply> list = carManageService.getMyCarApply(uid);
		return list;
	}
	
	@RequestMapping("getCarApplyManage")
	@ResponseBody
	public Map<String,Object> getCarApplyManage(CarApply carApply,Integer currentPage){
		System.out.println(carApply);
		Map<String,Object>result = carManageService.getCarApplyAll(carApply,currentPage);
		
		return result;
	}
	
	@RequestMapping("checkCarStatus")
	@ResponseBody
	public String checkCarStatus(CarApply carApply,HttpServletResponse response){
		System.out.println(carApply);
		response.setCharacterEncoding("utf-8");
		String result = carManageService.checkStatues(carApply);
		System.out.println(result);
		return result;
	}
	
	@RequestMapping("changeApplyStatue")
	@ResponseBody
	public String changeApplyStatue(Integer ca_id,String starte){
		Map<String,Object> map = new HashMap<>();
		map.put("ca_id", ca_id);
		map.put("starte", starte);
		String result = carManageService.changeApplyStatue(map);
		return result;
	}
	
	@RequestMapping("changeCarManage")
	@ResponseBody
	public String changeCarManage(CarManage carManage){
		String result = carManageService.changeCarManage(carManage);
		return result;
	}
	
}
